package com.example.inoculations.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.inoculations.entity.Forumcomment;
import com.example.inoculations.entity.Forum;
import com.example.inoculations.entity.User;
import com.example.inoculations.resultR.R;
import com.example.inoculations.service.IForumService;
import com.example.inoculations.service.IForumcommentService;
import com.example.inoculations.service.IUserService;
import com.example.inoculations.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author meirui
 * @since 2023-02-28
 */
@Controller
@ResponseBody
@RequestMapping("/inoculations/forumcomment")
public class ForumcommentController {
    @Autowired
    IForumcommentService iForumcommentService;
    @Autowired
    IForumService iForumService;
    @Autowired
    IUserService iUserService;
    /*分页查询*/
    @GetMapping("/pageall")
    public R<IPage<Forumcomment>> getpageall(@RequestParam("pageNum") Integer pageNum,
                                         @RequestParam("pageSize") Integer pageSize,
                                         @RequestParam(required = false) String forumid) {
        IPage<Forumcomment> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Forumcomment> queryWrapper = new QueryWrapper<>();
        /*根据身份证号模糊查询*/
        if (!StringUtils.isEmpty(forumid)){
            queryWrapper.like("forumid",forumid);
        }
        /*根据反馈时间倒序查询*/
//        queryWrapper.orderByDesc("submittime");
        queryWrapper.orderByDesc("id");//根据id序号倒序查询
        IPage<Forumcomment> page1 = iForumcommentService.page(page, queryWrapper);
        return R.SUCCESS(page1);
    }
    /*添加和修改，根据是否传入id，有则修改，无则添加*/
    @PostMapping("/save")
    public R forumcommentsave(@RequestBody Forumcomment forumcomment, HttpServletRequest request){
        String token = request.getHeader("Token");
        if(token!=null){
            String tokenId = JWTUtils.getIdByToken(token);
            User user = iUserService.getById(Integer.valueOf(tokenId));
            String name = user.getName();
            forumcomment.setCommentator(name);
        }
        if (iForumcommentService.saveOrUpdate(forumcomment)){
            //System.out.println("111111");
            Long forumid = forumcomment.getForumid();
            QueryWrapper<Forumcomment> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("forumid",forumid);
            Integer commenton = iForumcommentService.count(queryWrapper);
            UpdateWrapper wrapper = new UpdateWrapper();
            wrapper.eq("id", forumid);
            wrapper.set("commenton" , commenton);
            iForumService.update(wrapper);
            return R.SUCCESS();

        } else {
            return R.FAIL();
        }

    }
    /*删除*/
    @DeleteMapping("/del/{id}")
    public R forumcommentdel(@PathVariable Integer id){
        if (iForumcommentService.removeById(id)){
            return R.SUCCESS();
        }else {
            return R.FAIL();
        }
    }
}
